$(function () {
    // 初始化内容
    $("#myAlert").css('display', 'none');
    $("#taskMenu").attr("class", "active");
    $("#breadcrumb").html("定时任务配置");
    pagehtml(0);
    $("#btnSearch").click(function () {
        pagehtml(0);
    });

    $("#editTaskTimeBtn").click(function () {
        var id = $("#id").val();
        var cron = $("#cron").val();
        var enable = $("#enable").val();
        var jobName=$("#jobName").val();
        var basePath = getContextPath();
        $.post(basePath + "/back/task/edit",
            {id: id, cron: cron,enable:enable,jobName:jobName}, function (data) {
                $("#myAlert").css('display', 'block');
                $("#alertMsg").html(data.message);
                setTimeout(function () {
                    window.location.reload()
                }, 2000);
            })
    });
});

function setCron(obj) {
    $("#id").val(obj.id);
    $("#cron").val(obj.cron);
    $("#enable").val(obj.enable);
    $("#jobName").val(obj.jobName);
    $("#myModalLabel").text("修改任务执行时间");
    $('#myModal').modal();
}

function pagehtml(pageNum) {
    if (pageNum == 0) {
        pageNum = 1;
    } else {
        pageNum = pageNum;
    }
    var basePath = getContextPath();
    $.ajax({
        url: basePath + "/back/task/info",
        type: "post",
        dataType: "json",
        data: {pageNum: pageNum, pageSize: 20},
        success: function (data) {
            var options = {
                bootstrapMajorVersion: 3,//bootstrap版本
                alignment: "center",//居中显示
                currentPage: data.pageNum,//当前页数
                totalPages: data.totalPages,//总页数 注意不是总条数
                pageUrl: function (type, page, current) {
                    if (page == current) {
                        return "javascript:void(0)";
                    } else {
                        return "javascript:pagehtml(" + page + ")";
                    }
                }
            };
            var html = "";
            $.each(data.list, function (i, obj) {
                html += "<tr >" +
                    "<td>" + obj.id + "</td>" +
                    "<td>" + obj.jobName + "</td>"+
                    "<td>" + obj.cron + "</td>";
                if (obj.enable == '010') {
                    html += "<td><a href='#' class='btn btn-success'>启用中</a></td>";
                } else {
                    html += "<td><a href='#' class='btn btn-warning'>已停用</a></td>";
                }
                html +="<td>" + obj.createTime + "</td>" +
                    "<td>" + obj.updateTime + "</td>" +
                    "<td><a href='#' class='btn btn-success' " +
                    "onclick='setCron(" + JSON.stringify(obj) + ")'>修改任务</a></td>" +
                    "</tr>";
            });
            $("#content").empty();
            $("#content").append(html);
            $('#data-pagination').bootstrapPaginator(options);
        },
        error: function (error) {
            alert("error");
        }
    });
}
